package com.loongtech.bi.advertisment.controller;

import com.loongtech.bi.advertisment.model.ActiveUrlTranData;
import com.loongtech.bi.advertisment.service.AdvDayDataService;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName: AdvDayDataAction
 *
 * @author liuchen
 * @Description: 市场数据分析 —— 广告日数据（分url）
 * @date 2016-11-30 下午2:23:49
 */
@Controller("adDayReportAction")
@Scope("view")
public class AdvDayDataAction extends BaseAdvAction {
	private static final long serialVersionUID = 1L;

    private String dateRadio = "7"; // 切换的天数：1=昨天。7=近7天。15=近15天。30=近30天，0=自选时间

    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime()
            - 7 * Time.DAY)); // 自选时间：开始时间
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime()
            - Time.DAY)); // 自选时间：结束时间

    private List<ActiveUrlTranData> activeTranDatas;

    @Resource(name = "advDayDataService")
    private AdvDayDataService service;

    @PostConstruct
    public void init() {
        search();
    }

    public void search() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (hasAgents == null || hasAgents.size() <= 0) {
            context.addMessage(null, new FacesMessage("提示", "此用户无任何代理商权限，请联系管理员配置!"));
            return;
        }
        if ((selectedAgent == null || selectedAgent.length <= 0) && !"admin".equals(session.getUserName())) {
            context.addMessage(null, new FacesMessage("提示", "请选择代理商!"));
            return;
        }

        activeTranDatas = new ArrayList<>();
        try {
            String startDay = Time.getDateStr(new Date(Time
                    .getBeginningOfDay(("0".equals(dateRadio) ? dateStart
                            : new Date(Time.getBeginningOfDay(Time
                            .currentTime()
                            - (Integer.parseInt(dateRadio))
                            * Time.DAY))).getTime()))); // 开始时间
            String endDay = Time.getDateStr(new Date(Time
                    .getBeginningOfDay(("0".equals(dateRadio) ? dateEnd
                            : new Date(Time.getBeginningOfDay(Time
                            .currentTime() - Time.DAY))).getTime()))); // 结束时间
            activeTranDatas = service.getActiveTranDatas(startDay, endDay,
                    selectedAffiliation, selectedChannelName, selectedAgent,
                    selectedPlatform, selectedPaytype, selectedAdvPosition);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getDateRadio() {
        return dateRadio;
    }

    public void setDateRadio(String dateRadio) {
        this.dateRadio = dateRadio;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public List<ActiveUrlTranData> getActiveTranDatas() {
        return activeTranDatas;
    }

    public void setActiveTranDatas(List<ActiveUrlTranData> activeTranDatas) {
        this.activeTranDatas = activeTranDatas;
    }
}
